<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>秒杀上架</title>
    <!--导入样式-->
    <link rel="stylesheet" th:href="@{/css/register.css}">
    <!-- jquery -->
    <script type="text/javascript" th:src="@{/js/jquery.min.js}"></script>
    <!-- bootstrap -->
    <link rel="stylesheet" type="text/css"
          th:href="@{/bootstrap/css/bootstrap.min.css}"/>
    <script type="text/javascript" th:src="@{/bootstrap/js/bootstrap.min.js}">
    </script>
    <!-- jquery-validator -->
    <script type="text/javascript" th:src="@{/jquery-validation/jquery.validate.min.js}"></script>
    <script type="text/javascript" th:src="@{/jquery-validation/localization/messages_zh.min.js}"></script>
    <!-- layer -->
    <script type="text/javascript" th:src="@{/layer/layer.js}"></script>
    <!-- md5.js -->
    <script type="text/javascript" th:src="@{/js/md5.min.js}"></script>
    <!-- common.js -->
    <script type="text/javascript" th:src="@{/js/common.js}"></script>
</head>
<body>
<form name="seckillgroundForm" id="seckillgroundForm" method="post" style="width:50%; margin:0
auto">
    <h1 align="center">上&emsp;&emsp;架</h1>
    <table class="table" id="seckillground">
        <tr>
            <td>商品编号</td>
            <td>商品原价</td>
            <td>商品库存</td>
        </tr>
        <tr th:each="seckillground : ${seckillground}">
            <td th:id="goodsId" th:name="goodsId" th:text="${seckillground.id}"></td>
            <td th:id="goodsPrice" th:name="goodsPrice" th:text="${seckillground.goodsPrice}"></td>
            <td th:id="goodsStock" th:name="goodsStock" th:text="${seckillground.goodsStock}"></td>
        </tr>
    </table>
        <div class="form-group">
            <div class="row">
                <div class="col-md-5">
                    <p>秒杀价格：</p>
                </div>
                <div class="col-md-5">
                    <input id="seckillPrice" name="seckillPrice" class="form-control"
                           type="text" placeholder="秒杀价格应低于原价" required="true"
                           minlength="1" maxlength="30"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-5">
                    <p>秒杀数量：</p>
                </div>
                <div class="col-md-5">
                    <input id="stockCount" name="stockCount" class="form-control"
                           type="text" placeholder="秒杀数量应低于库存数量" required="true"
                           minlength="1" maxlength="30"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-5">
                    <p>秒杀开始时间：</p>
                </div>
                <div class="col-md-5">
                    <input id="startDate" name="startDate" class="form-control"
                           type="datetime-local" step="01" value-format="yyyy-MM-dd HH:mm:ss"
                           placeholder="秒杀开始时间" required="true"/>
                </div>
            </div>
        </div>
        <div class="form-group">
            <div class="row">
                <div class="col-md-5">
                    <p>秒杀结束时间：</p>
                </div>
                <div class="col-md-5">
                    <input id="endDate" name="endDate" class="form-control"
                           type="datetime-local" step="01" value-format="yyyy-MM-dd HH:mm:ss"
                           placeholder="秒杀结束时间" required="true"/>
                </div>
            </div>
        </div>
    <div class="row">
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="reset"
                    onclick="reset()">重置
            </button>
        </div>
        <div class="col-md-5">
            <button class="btn btn-primary btn-block" type="submit"
                    onclick="seckillground()">上架
            </button>
        </div>
    </div>
</form>
</body>
<script>
    function changedate(){
        var c=document.getElementById("seckillground");
        var goodsId=c.rows[1].cells[0].innerHTML;
        var goodsPrice=c.rows[1].cells[1].innerHTML;
        var goodsStock=c.rows[1].cells[2].innerHTML;

        let a=document.getElementById("startDate").value;
        let adate=a.substring(0,10);
        let atime=a.substring(11);
        let adt=adate+" "+atime;
        //a[10]=" ";

        var b=document.getElementById("endDate").value;
        let bdate=b.substring(0,10);
        let btime=b.substring(11);
        let a1=adate+atime;
        let bdt=bdate+" "+btime;
        //b[10]=" ";

        doseckillground(adt,bdt,goodsId,goodsPrice,goodsStock);
    }
    function seckillground(){
        /*上架校验*/
        $("#seckillgroundForm").validate({
            submitHandler: function (form) {
                /*校验成功，调用doseckillground*/
                changedate();
                // doseckillground();
            }
        });
    }
    function doseckillground(a,b,goodsId,goodsPrice,goodsStock){
        g_showLoading();/*加载图标转圈圈*/
        $.ajax({
            url: "/seckillground/doseckillground",/*调用后端接口*/
            type: "GET",
            data: {
                id:goodsId,
                // goodsId:$("#goodsId").val(),
                goodsPrice:goodsPrice,
                goodsStock:goodsStock,
                seckillPrice:$("#seckillPrice").val(),
                stockCount:$("#stockCount").val(),
                startDate:a,
                endDate:b
                // startDate:$("#startDate").val(),
                // endDate:$("#endDate").val()
            },
            success: function (data) {
                layer.closeAll();
                // console.log(data);
                // log.info("{}",data.seckillPrice);
                if (data.code == 200) {
                    //render(data.obj);
                    layer.msg("成功");
                    window.location.href = "/grounding/togrounding";
                } else {
                    layer.msg(data.message);
                }
            },
            error: function () {
                layer.closeAll();
            }
        });
    }
</script>
</html>